package top.aicats.blog.BlogManage.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import top.aicats.blog.BlogManage.model.ErrorCode;
import top.aicats.blog.BlogManage.model.ResultObj;
import top.aicats.blog.BlogManage.model.Users;
import top.aicats.blog.BlogManage.service.IUserManageService;

/**
 * @author sjc
 * @version 1.0
 * @date 2020/1/5 15:19
 * @email 952491025@qq.com
 */
@RestController
@RequestMapping(value = "/admin/userManage", method = RequestMethod.POST)
public class UserManageControl {

    @Autowired
    IUserManageService userManageServ;

    @PostMapping("/addUser")
    public ResultObj addUser(Users user) {
        ResultObj resultObj = new ResultObj();
        if (userManageServ.IsUserNameExist(user.getUserName())) {
            resultObj.setErrorCode(ErrorCode.ERROR);
            resultObj.setErrorContent("用户名已经存在");
            return resultObj;
        }
        int addResult = userManageServ.addOneUser(user);
        if (addResult > 0) {
            resultObj.setErrorCode(ErrorCode.OK);
            resultObj.setErrorContent("添加用户成功");
        } else {
            resultObj.setErrorCode(ErrorCode.ERROR);
            resultObj.setErrorContent("添加用户失败");
        }
        return resultObj;
    }
}
